package com.administrator.kotlindemo1

import android.content.Intent
import com.administrator.kotlindemo1.network.RetrofitManager
import com.administrator.kotlindemo1.utils.showToast
import com.hazz.kotlinmvp.net.exception.ErrorStatus
import com.hazz.kotlinmvp.net.exception.ExceptionHandle
import com.hazz.kotlinmvp.rx.scheduler.SchedulerUtils
import kotlinx.android.synthetic.main.activity_network.*

class NetworkActivity : BaseActivity() {


    override fun layoutId(): Int = R.layout.activity_network


    override fun initData() {
        var intent = this.intent?.extras
        val get = intent?.get("username")
//        showToast(get.toString())

    }

    override fun initView() {

        network.setOnClickListener({
            sendLogin()
            showDialog()
        })


        show_empty.setOnClickListener({
            multipleStatusView?.showEmpty()
        })

        recycler_btn.setOnClickListener({startActivity(Intent(this,RecyclerActivity::class.java))})
    }


    override fun start() {

    }

    fun sendLogin() {
        addSubscription(RetrofitManager.service.userlogin("588888", "123")
                .compose(SchedulerUtils.ioToMain())
                .subscribe({ ban ->
                    showToast(ban.toString())
                    dissmissDialog()

                }, { throwable ->
                    dissmissDialog()
                    showError(ExceptionHandle.handleException(throwable), ExceptionHandle.errorCode)
                }))
    }

    private fun showError(handleException: String, errorCode: Int) {
        showToast(handleException)
        if (errorCode == ErrorStatus.NETWORK_ERROR) {
            multipleStatusView?.showNoNetwork()
        } else {
            multipleStatusView?.showError()
        }
    }

}
